from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from app.core.config import settings

# from sqlalchemy.ext.asyncio import create_async_engine
# from sqlmodel.ext.asyncio.session import AsyncSession
# from sqlalchemy.pool import NullPool, QueuePool

DB_POOL_SIZE = 83
WEB_CONCURRENCY = 9
POOL_SIZE = max(DB_POOL_SIZE // WEB_CONCURRENCY, 5)
from typing import Generator

connect_args = {"check_same_thread": False}

print(settings.SQLALCHEMY_DATABASE_URI)
engine = create_engine(settings.SQLALCHEMY_DATABASE_URI, pool_pre_ping=True,echo=True)

SessionLocal = sessionmaker(
    autocommit=False,
    autoflush=False,
    bind=engine,
    expire_on_commit=False
)

def get_db() -> Generator:
    try:
        db = SessionLocal()
        yield db
    finally:
        db.close()

# asyn_engine = create_async_engine(
#     settings.SQLALCHEMY_DATABASE_URI,
#     echo=False,
#     future=True,
#     # pool_size=POOL_SIZE,
#     # max_overflow=64,
#     poolclass=NullPool
# )
#
# AsynSessionLocal = sessionmaker(
#     autocommit=False,
#     autoflush=False,
#     bind=asyn_engine,
#     expire_on_commit=False
# )
